/* 
 *  Copyright (C) 2008 Jens Meiß
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.hmvc4gwt.client.view.IViewGwt;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.BlogEntryController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.BlogEntryModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.BlogEntryView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.events.in.AddBlogEntryEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.events.in.ShowBlogViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogIdDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.IBlogRemoteService;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.IBlogRemoteServiceAsync;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.IBlogEntryRemoteService;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.IBlogEntryRemoteServiceAsync;
import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.TerminateEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * 
 * @author jens.meiss
 */
public class BlogController extends AControllerModelViewRegistry<BlogModel, BlogView> {

	private void addBlogEntry(IViewGwt view) {
		
		getView().getVerticalPanelBlogEntries().add(view.getWidget());
		
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == AddBlogEntryEvent.class) {
			
			addBlogEntry(((AddBlogEntryEvent) event).getView());
			
		} else if (event.getClass() == ShowBlogViewEvent.class) {
			
			fireEventGlobal(new AddApplicationViewEvent(getView().getLabelBlogTitle(), getView().getWidget()));
			
		}
		
	}

	public void init() {
		
		register(ShowBlogViewEvent.class);
		register(AddBlogEntryEvent.class);
		
		loadBlog();
		loadBlogEntries();
	}
	
	public void initModel() {
	}

	public void initView() {
		
		AbsolutePanel absolutePanelContainer = new AbsolutePanel();
		getView().setAbsolutePanelContainer(absolutePanelContainer);
		
		Label labelTitle = new Label();
		getView().setLabelBlogTitle(labelTitle);
		
		Label labelBlogEntries = new Label(I18N.constants.labelBlogEntries());
		getView().setLabelBlogEntries(labelBlogEntries);
		
		VerticalPanel verticalPanelBlogEntries = new VerticalPanel();
		verticalPanelBlogEntries.setStylePrimaryName("blogEntries");
		getView().setVerticalPanelBlogEntries(verticalPanelBlogEntries);
		
		/*
		 * build view
		 */
		absolutePanelContainer.add(verticalPanelBlogEntries);
	};
	
	/**
	 * 
	 */
	private void loadBlog() {
		
		IBlogRemoteServiceAsync async = (IBlogRemoteServiceAsync) GWT.create(IBlogRemoteService.class);
		async.getBlog(getModel().getBlogId(), new AsyncCallback<BlogDTO> () {
			public void onFailure(Throwable caught) {
				
				Window.alert(caught.getClass() + ": " + caught.getMessage());
				
			}
			public void onSuccess(BlogDTO blogDTO) {
				
				refreshBlog(blogDTO);
				
			}
			
		});
		
	}

	private void loadBlogEntries() {
		
		IBlogEntryRemoteServiceAsync async = (IBlogEntryRemoteServiceAsync ) GWT.create(IBlogEntryRemoteService.class);
		
		async.getListBlogEntry(new BlogIdDTO(getModel().getBlogId()), new AsyncCallback<List<BlogEntryDTO>>() {
			
			public void onFailure(Throwable caught) {
				Window.alert(caught.getClass() + ": " + caught.getMessage());
			};
			
			public void onSuccess(List<BlogEntryDTO> listBlogEntries) {
				refreshListBlogEntries(listBlogEntries);
				fireEventGlobal(new AddApplicationViewEvent(getView().getLabelBlogTitle(), getView().getWidget()));
			}
			
		});
	}

	private void refreshBlog(BlogDTO blogDTO) {
		
		getView().getLabelBlogTitle().setText(blogDTO.getTitle());
		getModel().setBlogDTO(blogDTO);
		
		
		
	}

	private void refreshListBlogEntries(
			List<BlogEntryDTO> listBlogEntries) {
		
		VerticalPanel verticalPanelBlogEntries = getView().getVerticalPanelBlogEntries();
		
		verticalPanelBlogEntries.add(getView().getLabelBlogEntries());
		
		fireEventDown(new TerminateEvent());
		
		for (BlogEntryDTO blogEntryDTO : listBlogEntries) {
			addChildComponent(new Component(new BlogEntryModel(blogEntryDTO), new BlogEntryView(), new BlogEntryController()));
		}
		
	}

	public void terminate() {
	}
}
